/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.PaymentsDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Payments;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.validator.Valid;

/**
 *
 * @author Sarah
 */
public class PostCollection extends ActionSupport implements ModelDriven<Collections> {

    @Valid
    private Collections coll;
    private Collections collsample;
    private AreaDAO ardao = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private LoansDAO loan = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private EmployeeDAO empdao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private CollectionsDAO colldao = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private PaymentsDAO paydao = (PaymentsDAO) ServletActionContext.getServletContext().getAttribute("paymentsDAO");
    private String cashonhand, totalc, cashshort;
    private int sequence;
    private String datecoll, acodecoll, ccodecoll;
    private String[] seriesname, bcodename, bnamename, receiptname, paymenttype;
    private String[] amountname;
    private String cshdepositnum;
    private int bankacctnum;
    private String cashdeposit;
    private String actstatus;
    private String loc;
    private String saved;

    public String getLoc() {
        return loc;
    }

    public String getActstatus() {
        return actstatus;
    }

    public void setActstatus(String actstatus) {
        this.actstatus = actstatus;
    }

    public void setPaymenttype(String[] paymenttype) {
        this.paymenttype = paymenttype;
    }

    public void setAmountname(String[] amountname) {
        this.amountname = amountname;
    }

    public void setBcodename(String[] bcodename) {
        this.bcodename = bcodename;
    }

    public void setBnamename(String[] bnamename) {
        this.bnamename = bnamename;
    }

    public void setReceiptname(String[] receiptname) {
        this.receiptname = receiptname;
    }

    public void setSeriesname(String[] seriesname) {
        this.seriesname = seriesname;
    }

    public void setAcodecoll(String acodecoll) {
        this.acodecoll = acodecoll;
    }

    public void setCcodecoll(String ccodecoll) {
        this.ccodecoll = ccodecoll;
    }

    public void setDatecoll(String datecoll) {
        this.datecoll = datecoll;
    }

    public void setBankacctnum(int bankacctnum) {
        this.bankacctnum = bankacctnum;
    }

    public void setCashdeposit(String cashdeposit) {
        this.cashdeposit = cashdeposit;
    }

    public void setCashonhand(String cashonhand) {
        this.cashonhand = cashonhand;
    }

    public void setCashshort(String cashshort) {
        this.cashshort = cashshort;
    }

    public void setCshdepositnum(String cshdepositnum) {
        this.cshdepositnum = cshdepositnum;
    }

    public void setTotalc(String totalc) {
        this.totalc = totalc;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

    public Collections getModel() {
        return coll;
    }

    @Action(value = "/post-coll", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {
            loc = "../../loancollection.jsp?cat=coll";

            collsample = colldao.get(sequence);

            if (collsample == null) {
                System.out.println("NULL YUNG COLLECTION");
            } else {


                System.out.println("UPDATE COLLECTION");
                //collsample
                Documents docupdate = collsample.getDocuments();
                docupdate.setDocumentno(null);
                docupdate.setDocumentdate(datecoll);

                Areas a = ardao.getByAreaCode(acodecoll);

                collsample.setCashonhand(Double.parseDouble(removeComma(cashonhand)));
                collsample.setShort_(Double.parseDouble(removeComma(cashshort)));
                collsample.setTotalamountcollected(Double.parseDouble(removeComma(totalc)));
                collsample.setDocuments(docupdate);
                collsample.setAreas(a);
                collsample.setDate(datecoll);
                collsample.setDepositamount(Double.parseDouble(removeComma(cashdeposit)));
                collsample.setDepositno(cshdepositnum);
                collsample.setStat("posted");

                List<Payments> paylist = paydao.getByCollectionID(collsample);
                Payments pym = null;
                double val = 0;

                if (paylist.size() > seriesname.length) {   //delete
                    for (int i = 0; i < paylist.size(); i++) {

                        if (i < seriesname.length) {
                            Loans l = loan.getLoansByBorrowerUnique(Integer.parseInt(bcodename[i]));
                            val = Double.parseDouble(removeComma(amountname[i]));
                            paylist.get(i).setCollections(collsample);
                            paylist.get(i).setLoans(l);
                            paylist.get(i).setAmount(val);
                            paylist.get(i).setReceiptno(Integer.parseInt(receiptname[i]));
                            paylist.get(i).setSeriesno(Integer.parseInt(seriesname[i]));
                            paylist.get(i).setType(paymenttype[i]);

                        } else {
                            paylist.get(i).setCollections(null);
                            paylist.get(i).setLoans(null);
                            paylist.get(i).setAmount(0);
                            paylist.get(i).setReceiptno(0);
                            paylist.get(i).setSeriesno(0);
                            paylist.get(i).setType("");

                        }

                    }
                } else {                                  //add or no change
                    for (int i = 0; i < seriesname.length; i++) {
                        Loans l = loan.getLoansByBorrowerUnique(Integer.parseInt(bcodename[i]));
                        val = Double.parseDouble(removeComma(amountname[i]));
                        double balance = l.getBalance();

                        if (i < paylist.size()) {
                            balance = balance - val;
                            paylist.get(i).setCollections(collsample);
                            paylist.get(i).setLoans(l);
                            paylist.get(i).setAmount(val);
                            paylist.get(i).setReceiptno(Integer.parseInt(receiptname[i]));
                            paylist.get(i).setSeriesno(Integer.parseInt(seriesname[i]));
                            paylist.get(i).setType(paymenttype[i]);
                            paylist.get(i).setBalance(balance);
                        } else {
                            pym = new Payments(collsample, l, Integer.parseInt(seriesname[i]), val, Integer.parseInt(receiptname[i]), paymenttype[i], balance, val);
                            paylist.add(pym);
                        }

                    }

                }



                saved = colldao.updateCollection(collsample, docupdate, paylist);
            }


            if (saved.equals("ok")) {
                actstatus = "ok";
            } else {
                actstatus = saved;
            }

            ServletActionContext.getRequest().setAttribute("savedeposit", actstatus);
            return SUCCESS;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            actstatus = e.toString();
            System.out.println("SA SAVE DEPOSIT ETO" + e.toString());
        }
        return SUCCESS;
    }

    public String removeComma(String money) {
        String[] split = money.split(",");
        String splitstring = "";
        for (int j = 0; j < split.length; j++) {
            splitstring += split[j];
        }
        return splitstring;
    }
}
